package memento;

/**
 * @Author: Ma JiBin
 * @Email: 1423553596@qq.com
 * @Date: 2018/9/12 15:58
 * @Description: 备忘录模式（Memento Pattern）
 * Memento：为保存对象状态的类
 * Originator：该类对象的状态被保存
 * CareTaker：将保存的状态存入该类对象中
 */
public class Main {
    public static void main(String[] args) {
        Originator originator = new Originator();
        CareTaker careTaker = new CareTaker();
        originator.setState("State #1");
        originator.setState("State #2");
        careTaker.add(originator.saveStateToMemento());
        originator.setState("State #3");
        careTaker.add(originator.saveStateToMemento());
        originator.setState("State #4");

        //获取originator对象当前状态
        System.out.println("Current State: " + originator.getState());
        //获取originator对象第一次保存的状态
        originator.getStateFromMemento(careTaker.get(0));
        System.out.println("First saved State: " + originator.getState());
        //获取originator对象第二次保存的状态
        originator.getStateFromMemento(careTaker.get(1));
        System.out.println("Second saved State: " + originator.getState());
    }
}
